﻿/* Write a method that calculates the number of workdays between today and given date, passed as parameter. 
 * Consider that workdays are all days from Monday to Friday except a fixed list of public holidays specified 
 * preliminary as array.
 */

using System;
using System.Globalization;
using System.Threading;

namespace _5.CalcTimeframe
{
    class CalculateTimeframe
    {
        static string dateformat = "dd.MM.yyyy";
        static void Main()
        {
            //set globalization settings
            Thread.CurrentThread.CurrentCulture = new CultureInfo("bg-BG");
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            
            //define holidays
            string[] holidays_ = {
                                     "05.02.2013", "06.02.2013", "07.02.2013", 
                                     "20.02.2013", "27.02.2013", "01.04.2013" };
            DateTime[] holidays = new DateTime[holidays_.Length];
            for (int i = 0; i < holidays_.Length; i++)
            {
                //string parseStr = holidays_[i] + " г. 00:00:00 ч.";
                //pass exceptions like 30th of February, 30th of March, 0.04 etc.
                if (DateTime.TryParseExact(holidays_[i], "dd.MM.yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out holidays[i]) == false)
                {
                    Console.Write("Error!: Invalid date or there is no such date in the holidays predefined array.");
                    throw new System.FormatException("Please check the holidays_ array. An invalid entry detected. (Probably there is no such date in the gregorian calendar or an example reason for this message might be the date 30.02.2013, or a date in the format 31.<month> passed to the input in a month which contains 30 days. \r\n(or maybe you have provided a leap date in a year which is not leap? tc tc)");
                    //Environment.Exit(-1);
                    break;
                }
                //holidays[i] = DateTime.Parse(holidays_[i], CultureInfo.CurrentCulture);
            }

            DateTime givendate;
            bool iProvided = false;
            do
            {
                if (iProvided == true) Console.WriteLine("An invalid date is entered! Please try again.\r\n\tValid input going from {0} to {1}\r\n* The date should be provided in the following format: \r\ndd.MM.yyyy\r\n", DateTime.MinValue, DateTime.MaxValue);
                else iProvided = true;
                Console.Write("Date: ");
            } while (DateTime.TryParseExact(Console.ReadLine(), "dd.MM.yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out givendate) == false);
            //Console.WriteLine(givendate.ToString());
            
            //count and check for workdays/holidays
            bool is_not_a_workday = false;
            int count_workdays = 0;
            

            for (var day = DateTime.Today; day <= givendate; day = day.AddDays(1))
            {
                is_not_a_workday = false; //false by default
                //skip if saturday or sunday
                if (day.DayOfWeek == DayOfWeek.Saturday || day.DayOfWeek == DayOfWeek.Sunday) is_not_a_workday = true;
                foreach (var val in holidays)
                {
                    if (DateTime.Compare(val, day) == 0)
                    {
                        Console.WriteLine("{0} is a holiday, skipping", val.ToString(dateformat));
                        is_not_a_workday = true;
                    }
                    //Console.WriteLine(val.ToString());
                }
                if (is_not_a_workday == false) count_workdays++;
            }

            Console.WriteLine("Workdays between today({0}) and {1} are {2}", DateTime.Now.ToString(dateformat), givendate.ToString(dateformat), count_workdays);
        }
    }
}
